package com.offcn.dao;

import com.offcn.entity.User;
import com.offcn.utils.BaseDao;

import java.util.List;

public class UserDaoImpl extends BaseDao<User> implements UserDao {
    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User login(String username, String password) {
        return getFindOne("select * from user where username = ? and password = ?",User.class,username,password);
    }

    /**
     * 添加
     * @param user
     * @return
     */
    @Override
    public int add(User user) {
        return update("insert into user values(null,?,?,?,?,?,?,?,now(),?,null)"
                ,user.getName(),user.getPhone(),user.getAge(),user.getSex(),user.getUsername(),
                user.getPassword(),user.getStatus(),user.getRole());
    }

    /**
     * 分页查询
     * @param sql
     * @param o
     * @return
     */
    @Override
    public List<User> search(String sql, Object... o) {
        return getFindMore(sql,User.class,o);
    }

    /**
     * update 表名 set 列名1=新值1,列名2=新值2,... where 条件;
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public int update(User user) {
        return update("update user set name = ?,phone = ?,age = ?,sex = ?,username = ?,password = ?,status = ?,role = ? where uid = ?",
                user.getName(),user.getPhone(),user.getAge(),user.getSex(),user.getUsername(),user.getPassword(),user.getStatus(),user.getRole(),user.getUid());
    }

    /**
     * 删除用户信息
     * @param uid
     * @return
     */
    @Override
    public int del(String uid) {
        return update("delete from user where uid = ?",uid);
    }

    /**
     * 根据id查询用户信息
     * @param uid
     * @return
     */
    @Override
    public User getUser(int uid) {
        return getFindOne("select * from user where uid = ?",User.class,uid);
    }
}
